﻿/*This file is part of ILoveHUSTBoard.

ILoveHUSTBoard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ILoveHUSTBoard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ILoveHUSTBoard.  If not, see <http://www.gnu.org/licenses/>.*/

var _username="error";
var _hasreply=[];

function toggle_messageform(){
	var $box=$("#messageform");
	$box.slideToggle("fast");
}

function toggle_replyform(elem){
	var $box=$(elem).parent().parent().find(".replyform");
	var $replys=$(elem).parent().parent().find(".message");
	$box.slideToggle("fast");
	$replys.slideToggle("fast");
}

function addreply(eleme){
	var elem=eleme;
	var messageid=$(elem).parent().parent().parent().find("[name='messageid']").val();
	var reply=$(elem).parent().find("[name='message']").val();
	if(reply==""){
		alert("回复内容不能为空");
		return false;
	}
	$.get("actions/add_reply.aspx", { 'message_id': messageid, 'reply': reply }, function (response) {
	    if (response != "error" && response != "fail" && response != "hexie") {
	        var replycount = parseInt($(elem).parent().parent().parent().find(".replycount").html());
	        $(elem).parent().parent().parent().find(".replycount").html("" + (replycount + 1));
	        var $reply = $("<div class='message'></div>");
	        $reply.append("<strong>" + response + "</strong>");
	        $reply.append("<span>" + reply + "</span>");
	        $(elem).parent().parent().parent().find(".share_div").after($reply);
	        $(elem).parent().find("[name='message']").val("");
	    } else {
	        if (response == "hexie") {
	            alert("请使用文明用语");
	        }
	        else {
	            alert("请登录后回复");
	        }
	    }
	});
}

function add_love(elem){
	var id=$(elem).parent().parent().find("[name='messageid']").val();
	$.get("actions/add_love_count.aspx",{'id':id},function(response){
		if(response!="-1"){
			$(elem).parent().find(".lovecount").html(response);
			$(elem).unbind("click");
		}else{
			alert("处理失败");
			return false;
		}
	});
}

function getusername(){
	$.ajax({
		url:"actions/get_username.aspx",
		async:false,
		complete:function(response){
			_username=response;
		}
	});
	return _username;
}

function islogin(){
	$.ajax({
		url:"actions/get_username.aspx",
		async:false,
		complete:function(response){
			_username = response.responseText;
		}
	});
	return _username != "error";
}

$(document).ready(function(){
	$(window).scroll(function(e){
		if(document.body.scrollTop==0){
			$(".top").hide();
		}else{
			$(".top").show();
		}
	});
	$(".share").click(function(){
		$(this).parent().parent().find(".share_div").slideToggle("fast");
	});
	$(".myword").click(function(e){
		toggle_messageform();
		return false;
	});
	$(".toreply").click(function(e){
		$(this).parent().parent().find(".share_div").slideUp("fast");
		toggle_replyform(this);
		return false;
	});
	$(".tolove").click(function(e){
		add_love(this);
		return false;
	});
	$(".close").click(function(e){
		$(".loginbox").hide();
	});
	$(".tosubmitreply").click(function(e){
		if(!islogin()){
			if($(".loginbox").is(":hidden")){
				$(".loginbox").show();
			}
			return false;
		}
		addreply(this);
		return false;
	});
	$(".tologin").click(function(e){
		if($(".loginbox").is(":hidden")){
			$(".loginbox").show();
	 	}
		return false;
	});
});